package com.zbkj.front.service.impl;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.entity.user.UserAddress;
import com.zbkj.front.model.vo.PickupTimeAvailableCheckVO;
import com.zbkj.front.model.vo.PickupTimeVO;
import com.zbkj.front.service.FrontExpressService;
import com.zbkj.service.service.UserAddressService;
import com.zbkj.service.sfexpress.CallExpressNewAPIService;
import com.zbkj.service.sfexpress.pickup.PickupTimeV1_1Res;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class FrontFrontExpressServiceImpl implements FrontExpressService {

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private CallExpressNewAPIService callExpressNewApiService;

    @Override
    public PickupTimeVO getAvailablePickupTimeOfCity(Integer userAddressId) {
        UserAddress detail = userAddressService.getDetail(userAddressId);
        if (Objects.isNull(detail)) {
            throw new CrmebException("用户地址数据不存在");
        }
        PickupTimeVO pickupTimeVO = new PickupTimeVO();
        pickupTimeVO.setMessage(null);
        pickupTimeVO.setStartTime(LocalDate.now().atTime(8, 0, 0));
        pickupTimeVO.setEndTime(LocalDate.now().atTime(19, 0, 0));
        return pickupTimeVO;
    }

    @Override
    public PickupTimeAvailableCheckVO checkAvailablePickupTimeOfCity(Integer userAddressId, LocalDateTime sendTime) {
        UserAddress detail = userAddressService.getDetail(userAddressId);
        if (Objects.isNull(detail)) {
            throw new CrmebException("用户地址数据不存在");
        }
        String province = detail.getProvince();
        String city = detail.getCity();
        String county = detail.getDistrict();
        PickupTimeV1_1Res pickupTimeV11Res = callExpressNewApiService.checkPickupTime(detail.getDetail(), detail.getCityId().toString(), sendTime, province, city, county);
        PickupTimeAvailableCheckVO pickupTimeAvailableCheckVO = new PickupTimeAvailableCheckVO();
        pickupTimeAvailableCheckVO.setAvailable(pickupTimeV11Res.getStatus());
        pickupTimeAvailableCheckVO.setStartTm(pickupTimeV11Res.getStartTm());
        pickupTimeAvailableCheckVO.setEndTm(pickupTimeV11Res.getEndTm());
        pickupTimeAvailableCheckVO.setMessage(null);
        return pickupTimeAvailableCheckVO;
    }
}
